﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CodeFirstDomain.ModelContext;
using Framework.Services;
using CodeFirstDomain.IService;
using Framework.Infrastructure;
using Framework.UnitOfWork;
using CodeFirstDomain.Model;

namespace CodeFirstDomain.Service
{
    class AppService : ApplicationService, IAppService
    {
        public AppService(IUnitOfWork uow) : base(uow) { }

        #region IAppService Members

        public void Test()
        {
            DoTest();
            UnitOfWork.Commit();
        }

        internal void DoTest()
        {
            var loan = DomainRepository.FindAll<Loan>().FirstOrDefault();
            var share = new Share { Company = "ivan", Amount = 1 };

            if (loan == null)
            {
                loan = new Loan();
                
                //loan.Shares = new SharePie();
                loan.Shares = new SharePie(new List<Share>(new Share[] { new Share { Company = "ivan" + DateTime.Now.ToString(), Amount = 1 } }));
                DomainRepository.Add(loan);
                UnitOfWork.Commit();
            }

            
          

            //loan.Shares.Shares.Remove(share);
            //loan.Shares.MarkAsDeleted();
            loan.Shares = new SharePie(new List<Share>(new Share[] { new Share { Company = "ivan" + DateTime.Now.ToString(), Amount = 1 } }));
           
            UnitOfWork.Commit();

            return;
            //var userRepository = GetRepository<User>();
            //var userID = new Guid("b3c32fc5-33fa-4c74-86bc-19baba8b756f");
            //var user = userRepository.GetByKey(userID);
            //if (user == null)
            //{

            //    user = new User { ID = userID, Address = new Address(DateTime.Now.ToShortDateString(), "building1") };
               
            //    userRepository.Add(user);
            //    UnitOfWork.Commit();
            //}
            //userRepository.Remove(user);
            //RepositoryContext.Commit();
            //var mblogs = user.SpecialMBlogs;
            //var list = mblogs.ToList();
            //user.MBlogs.Add(new MBlog { ID = Guid.NewGuid(), Body = "blog1", CreateTime = DateTime.Now, Status = EnableStatus.Normal });
            //user.MBlogs.Add(new MBlog { ID = Guid.NewGuid(), Body = "blog2", CreateTime = DateTime.Now, Status = EnableStatus.Normal });
            //user.MBlogs.Add(new MBlog { ID = Guid.NewGuid(), Body = "blog3", CreateTime = DateTime.Now, Status = EnableStatus.Normal });
          //  user.Address = new Address(DateTime.Now.ToShortDateString(), "building1");
          //  var query = user.GetQueryable(u => u.MBlogs);
          //  List<MBlog> mblogs = query.ToList();

          //  var mblog = new MBlog { /*Author = user,*/ ID = Guid.NewGuid(), Body = "blog1", CreateTime = DateTime.Now, Status = EnableStatus.Normal };
          //  //mblog.MarkAsAdded();
          //  user.AddMBlog(mblog);
          ////  mblog.Added();
          //  user.CollectionLoad(u => u.MBlogs);
          
          //  UnitOfWork.Commit();
        }

        #endregion
        
    }
}
